<?php
/**
 * Created by PhpStorm.
 * User: HuangLuSen
 * Date: 2017/5/4
 * Time: 19:30
 */

namespace app\admin\model;


use think\Model;
use app\admin\controller\Auth;
use tree\Tree;
use think\facade\Cache;
class AuthRule extends Model
{
    protected $not_check_id=[100];//不检测权限的管理员id
    protected $not_check_url=['admin/index/index','admin/sys/clear','admin/index/lang','mobile/index/index','mobile/ajax/region'];//不检测权限的url
    protected $autoWriteTimestamp = 'int';
    public function getStatusTextAttr($value,$data)
    {
        $status = [1=>'<span class="label label-success">显示</span>',0=>'<span class="label label-danger">隐藏'];
        return $status[$data['status']];
    }
    public function getNotcheckTextAttr($value,$data)
    {
        $status = [1=>'<span class="label label-danger">No测</span>',0=>'<span class="label label-success">检测'];
        return $status[$data['notcheck']];
    }
    /**
     * 获取指定url的id(可能为显示状态或非显示状态)
     * @param string $url 为空获取当前操作的id
     * @param int $status 1表示取显示状态,为空或为0则不限制
     * @return int -1表示不需要检测 0表示无后台菜单 其他表示当前url对应id
     */
    public function get_url_id($url='',$status=0)
    {
        $url=$url?:request()->module().'/'.request()->controller().'/'.request()->action();
        $url = strtolower($url);
        if(in_array($url,$this->not_check_url)) return -1;
        $where['name']=$url;
        if($status) $where['status']=$status;
        $menu_id = self::where($where)->order('level desc,sort')->value('id');//4级或3级(如果4级,status是0,不显示)
        $menu_id=$menu_id?:0;
        return $menu_id;
    }
    /**
     * 权限检测
     * @param int
     * @return boolean
     */
    public function check_auth($id=0,$mobile=false)
    {
        if(is_string($id) && !empty($id)){
            $id=$this->get_url_id($id);
        }else{
            $id=$id?:$this->get_url_id();
        }
        if($id==-1) return true;
        $uid=session('admin_user.adminid');
        if(in_array($uid,$this->not_check_id)) return true;
        $auth_ids_list=Cache::tag('menu')->get('auth_ids_list_'.$uid);
        if(empty($auth_ids_list)){
            $auth = new Auth();
            $auth_ids_list=$auth->getAuthList($uid,1,'id');
            Cache::tag('menu')->set('auth_ids_list_'.$uid,$auth_ids_list);
        }
        if(empty($auth_ids_list)) return false;
        if(in_array($id,$auth_ids_list)){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 手机端权限检测
     * @return array
     */
    public function mobilecheck()
    {
        $auth_ids_list=Cache::tag('menu')->get('auth_ids_list_'.UID);
        if(empty($auth_ids_list)){
            $auth = new Auth();
            $auth_ids_list=$auth->getAuthList(UID,1,'id');
            Cache::tag('menu')->set('auth_ids_list_'.UID,$auth_ids_list);
        }
        return $auth_ids_list;
    }
    /**
     * 获取权限菜单
     * @return array
     */
    public function get_admin_menus()
    {
        $uid=session('admin_user.adminid');
        $menus=Cache::tag('menu')->get('menus_admin_'.$uid);
        if($menus) return $menus;
        $where['status']=1;
        if(!in_array($uid,$this->not_check_id)){
            $auth_ids_list=Cache::tag('menu')->get('auth_ids_list_'.$uid);
            if(empty($auth_ids_list)){
                $auth = new Auth();
                $auth_ids_list=$auth->getAuthList($uid,1,'id');
                Cache::tag('menu')->set('auth_ids_list_'.$uid,$auth_ids_list);
            }
            if(empty($auth_ids_list)) return [];
            $where['id']=array('in',$auth_ids_list);
        }
        $data = self::where($where)->order('sort')->select();
        $tree=new Tree();
        $tree->init($data,['child'=>'_child','parentid'=>'pid']);
        $menus=$tree->get_arraylist($data);
        Cache::tag('menu')->set('menus_admin_'.$uid,$menus);
        return $menus;
    }
    /**
     * 获取权限菜单
     * @return array
     */
    public static function get_ruels_tree()
    {
        $data=self::order('sort')->select();
        $data=$data->toArray();
        $tree=new Tree();
        $tree->init($data,['child'=>'sub','parentid'=>'pid']);
        $rst=$tree->get_arraylist($data);
        return $rst;
    }

}